package com.example.campus.controller.admin;


import com.example.campus.pojo.dto.*;
import com.example.campus.pojo.entity.Admin;
import com.example.campus.pojo.vo.AdminLoginVO;
import com.example.campus.pojo.vo.StatisticsVO;
import com.example.campus.properties.JwtProperties;
import com.example.campus.result.PageResult;
import com.example.campus.result.Result;
import com.example.campus.service.AdminService;
import com.example.campus.utils.JwtUtil;
import com.example.campus.utils.constant.JwtClaimsConstant;
import com.example.campus.utils.constant.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员接口
 */
@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags = "管理端管理员相关接口")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/register")
    @ApiOperation("管理员注册")
    public Result register(@RequestBody @Validated AdminRegisterDTO adminRegisterDTO) {
        adminService.register(adminRegisterDTO);
        return Result.success(MessageConstant.REGISTER_SUCCESS);
    }

    @PostMapping("/login")
    @ApiOperation(value = "管理员登录")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("管理员登录：{}", adminLoginDTO);

        Admin admin = adminService.login(adminLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ADMIN_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .userName(admin.getUsername())
                .name(admin.getName())
                .token(token)
                .build();

        return Result.success(adminLoginVO, MessageConstant.LOGIN_SUCCESS);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "管理员退出")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping("/add")
    @ApiOperation("新增管理员")
    public Result add(@RequestBody @Validated AdminAddDTO adminDTO) {
        log.info("新增管理员：{}", adminDTO);
        adminService.add(adminDTO);
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    @DeleteMapping("/delete/{adminId}")
    @ApiOperation("删除管理员")
    public Result delete(@PathVariable Long adminId) {
        adminService.delete(adminId);
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }

    @GetMapping("/list")
    @ApiOperation("管理员分页查询")
    public Result<PageResult> page(AdminPageQueryDTO adminPageQueryDTO) {
        log.info("管理员分页查询，参数为：{}", adminPageQueryDTO);
        PageResult pageResult = adminService.pageQuery(adminPageQueryDTO);
        return Result.success(pageResult, MessageConstant.GET_LIST_SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("更新管理员信息")
    public Result update(@RequestBody @Validated AdminUpdateDTO adminUpdateDTO) {
        adminService.update(adminUpdateDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @PutMapping("/statusChange")
    @ApiOperation("修改管理员状态")
    public Result changeAdminStatus(@RequestBody AdminStatusChangeDTO adminStatusChangeDTO) {
        adminService.changeAdminStatus(adminStatusChangeDTO);
        return Result.success(MessageConstant.CHANGE_ADMIN_STATUS_SUCCESS);
    }

    @PutMapping("/updatePwd")
    @ApiOperation("更新已登录管理员的密码")
    public Result updatePassword(@RequestBody @Validated PwdUpdateDTO pwdUpdateDTO) {
        adminService.updatePwd(pwdUpdateDTO);
        return Result.success(MessageConstant.UPDATE_PWD_SUCCESS);
    }

    @GetMapping("/statistics")
    @ApiOperation("获得统计数据")
    public Result statistics() {
        StatisticsVO statisticsVO = adminService.getStatistics();
        return Result.success(statisticsVO, MessageConstant.GET_STATISTICS_SUCCESS);
    }
}
